package com.hkj.genealogy.genealogydemo.config;

import com.hkj.genealogy.genealogydemo.service.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @program: genealogydemo
 * @description: 安全配置类
 * @author: Hekejian
 * @time: 2020-07-04 22:59
 **/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Autowired
    MyUserDetailsService myUserDetailsService;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/")
                .permitAll()
                .antMatchers("/login")
                .permitAll()
                .antMatchers("/register")
                .permitAll()
                .antMatchers("/static/**")
                .permitAll()
                .antMatchers("/doRegister")
                .permitAll()
                .antMatchers("/success")
                .permitAll()
                .antMatchers("/home")
                .authenticated()
                .antMatchers("/createRoot")
                .authenticated()
                .antMatchers("/addRoot")
                .authenticated()
                .antMatchers("/createChildren")
                .authenticated()
                .antMatchers("/addChildren")
                .authenticated()
                .antMatchers("/getGenealogy")
                .authenticated()
                .antMatchers("/getGenealogyPage")
                .authenticated();
        http.formLogin().usernameParameter("username").passwordParameter("password")
                .loginProcessingUrl("/login")
                .loginPage("/").defaultSuccessUrl("/home");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(myUserDetailsService);
    }
}
